;;; guix-config --- additional software for a custom configuration of gnu-guix
;;; Copyright © 2020 Kolya <kolya.daemon@vfemail.net>
;;;
;;; This file is part of guix-config
;;;
;;; guix-config is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; guix-config is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (kolyad packages firmware)
  #:use-module (gnu packages base)
  #:use-module (gnu packages bison)
  #:use-module (gnu packages compression)
  #:use-module (gnu packages firmware)
  #:use-module (gnu packages flex)
  #:use-module (guix packages)
  #:use-module (guix build-system gnu)
  #:use-module (guix build-system trivial)
  #:use-module (guix utils)
  #:use-module (guix download)
  #:use-module (guix git-download)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module ((nonguix licenses) #:prefix license:)
  #:use-module (srfi srfi-1))

(define-public b43-ssb-sprom
  (let ((commit "27892ef741e7f1d08cb939744f8b8f5dac7b04ae")
        (revision "1"))
    (package
      (name "b43-ssb-sprom")
      (version (git-version "0.0.0" revision commit))
      (source
       (origin
         (method git-fetch)
         (uri (git-reference
               (url "http://git.bues.ch/git/b43-tools.git")
               (commit commit)))
         (file-name (git-file-name name version))
         (sha256
          (base32
           "1wgmj4d65izbhprwb5bcwimc2ryv19b9066lqzy4sa5m6wncm9cn"))))
      (build-system gnu-build-system)
      (native-inputs
       `(("flex" ,flex)
         ("bison" ,bison)))
      (arguments
       `(#:modules ((srfi srfi-1)
                    (guix build gnu-build-system)
                    (guix build utils))
         #:tests? #f                    ; no tests
         #:phases
         (let ((subdir "ssb_sprom"))
           (modify-phases %standard-phases
             (delete 'configure)        ; no configure script
             (add-before 'build 'patch-/bin/true
               (lambda _
                 (substitute* (find-files "." "Makefile")
                   (("/bin/true") ":"))
                 #t))
             (replace 'build
               (lambda _
                 (invoke "make" "-C" subdir "CC=gcc")))
             (replace 'install
               (lambda* (#:key outputs #:allow-other-keys)
                 (let* ((out (assoc-ref %outputs "out"))
                        (install (string-append out "/bin")))
                   (install-file (string-append subdir "/ssb-sprom") install))))))))
      (home-page
       "https://bues.ch/cms/hacking/misc.html#linux_b43_driver_firmware_tools")
      (synopsis "Modify Broadcom Sonics Silicon Backplane SPROM")
      (description
"A tool for the convenient modification of the Broadcom Silicon Sonics Backplane SPROM.
This Tool automatically adjusts the CRC after the modification")
      (license license:gpl2))))

(define-public b43-fwcutter
  (let ((commit "27892ef741e7f1d08cb939744f8b8f5dac7b04ae")
        (revision "1"))
    (package
      (name "b43-fwcutter")
      (version (git-version "019" revision commit))            ; XXX: A tarball exists, should I use that?
      (source
       (origin
         (method git-fetch)
         (uri (git-reference
               (url "http://git.bues.ch/git/b43-tools.git")
               (commit commit)))
         (file-name (git-file-name name version))
         (sha256
          (base32
           "1wgmj4d65izbhprwb5bcwimc2ryv19b9066lqzy4sa5m6wncm9cn"))))
      (build-system gnu-build-system)
      (native-inputs
       `(("flex" ,flex)
         ("bison" ,bison)))
      (arguments
       `(#:modules ((srfi srfi-1)
                    (guix build gnu-build-system)
                    (guix build utils))
         #:tests? #f                    ; no tests
         #:phases
         (let ((subdir "fwcutter"))
           (modify-phases %standard-phases
             (delete 'configure)        ; no configure script
             (add-before 'build 'patch-/bin/true
               (lambda _
                 (substitute* (find-files "." "Makefile")
                   (("/bin/true") ":"))
                 #t))
             (replace 'build
               (lambda _
                 (invoke "make" "-C" subdir "CC=gcc")))
             (replace 'install
               (lambda* (#:key outputs #:allow-other-keys)
                 (let* ((out (assoc-ref outputs "out"))
                        (bin (string-append out "/bin"))
                        (man1 (string-append out "/share/man/man1"))
                        (bin-file (string-append subdir "/b43-fwcutter"))
                        (man-file (string-append subdir "/b43-fwcutter.1")))
                   (install-file bin-file bin)
                   (install-file man-file man1))))))))
      (home-page
       "https://bues.ch/cms/hacking/misc.html#linux_b43_driver_firmware_tools")
      (synopsis "Extract firmware for the b43 driver")
      (description "b43-fwcutter is a tool to extract firmware from binary Broadcom 43xx driver files.")
      (license license:bsd-2))))

(define-public b43-firmware
  (package
    (name "b43-firmware")
    (version "6.30.163.46")
    (source (origin
              (method url-fetch)
              (uri (string-append "https://www.lwfinger.com/b43-firmware/" 
                                  "broadcom-wl-" version ".tar.bz2"))
              (sha256
                (base32 "0baw6gcnrhxbb447msv34xg6rmlcj0gm3ahxwvdwfcvq4xmknz50"))))
    (build-system trivial-build-system)
    (arguments
      `(#:modules ((guix build utils))
        #:builder
        (begin
          (use-modules (guix build utils))
          (let ((PATH (string-append (assoc-ref %build-inputs "bzip2") "/bin:"
                                     (assoc-ref %build-inputs "tar") "/bin:"
                                     (assoc-ref %build-inputs "b43-fwcutter") "/bin"))
                (source (assoc-ref %build-inputs "source"))
                (firmware-dir (string-append %output "/lib/firmware")))
            (setenv "PATH" PATH)
            (invoke "tar" "xvjf" source)               ; XXX: This doesn't look very good, it shouldn't be dependent on filenames like this.
            (mkdir-p firmware-dir)                     ; XXX:  this will be easy to fix with updated url-fetch/tarbomb origin method compatible with bzip2 files.
            (invoke "b43-fwcutter" "-w" firmware-dir (string-append "broadcom-wl-" ,version ".wl_apsta.o"))))))
    (native-inputs
      `(("b43-fwcutter" ,b43-fwcutter)
        ("tar" ,tar)
        ("bzip2" ,bzip2)))
    (home-page "https://wireless.wiki.kernel.org/en/users/Drivers/b43")
    (synopsis "Firmware for the b43 driver")
    (description "Firmware for Broadcom 43xx wireless networking chips")
    (license
      (license:undistributable "https://www.broadcom.com/support/download-search?pf=Wireless+LAN+Infrastructure"))))

;;; vim: set expandtab tabstop=2 shiftwidth=2
;;; end of file firmware.scm
